package com.forum.module.system.controller.admin.auth;

import cn.hutool.core.util.StrUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.security.config.SecurityProperties;
import com.forum.framework.security.core.util.SecurityUtils;
import com.forum.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import com.forum.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.forum.module.system.controller.admin.auth.vo.AuthRegisterReqVO;
import com.forum.module.system.controller.admin.auth.vo.AuthRegisterRespVO;
import com.forum.module.system.dal.dataobject.user.UserDO;
import com.forum.module.system.service.auth.AdminAuthService;
import com.forum.module.system.service.user.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

import static com.forum.framework.common.pojo.DataResult.success;
import static com.forum.framework.security.core.util.SecurityUtils.getLoginUserId;

/**
 * @author zihan.ouyang
 */
@Tag(name = "管理后台 - 认证")
@Validated
@RestController("adminAuthController")
@RequestMapping("/system/auth")
public class AuthController {
    private static final Logger log = LoggerFactory.getLogger(AuthController.class);

    private final AdminAuthService authService;

    private final UserService userService;

    private final SecurityProperties securityProperties;

    public AuthController(AdminAuthService authService,
                          UserService userService,
                          SecurityProperties securityProperties) {
        this.authService = authService;
        this.userService = userService;
        this.securityProperties = securityProperties;
    }

    @PostMapping("/login")
    @Operation(summary = "使用账号密码登录")
    public DataResult<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) {
        return success(authService.login(reqVO));
    }

    @PostMapping("/refresh-token")
    @Operation(summary = "刷新令牌")
    @Parameter(name = "refreshToken", description = "刷新令牌", required = true)
    public DataResult<AuthLoginRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        return success(authService.refreshToken(refreshToken));
    }

    @GetMapping("/get-permission-info")
    @Operation(summary = "获取登录用户的权限信息")
    public DataResult<?> getPermissionInfo() {
        // 1.1 获得用户信息
        UserDO user = userService.getUser(getLoginUserId());
        if (user == null) {
            return success(null);
        }

        // 1.2 获得角色列表

        return success(user);
    }

    @PostMapping("/register")
    @Operation(summary = "注册用户")
    public DataResult<AuthRegisterRespVO> register(@RequestBody @Valid AuthRegisterReqVO reqVO) {
        return success(authService.register(reqVO));
    }

    @PostMapping("/logout")
    @Operation(summary = "退出登录")
    public DataResult<Boolean> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request, securityProperties.getHeaderName(),
                securityProperties.getTokenParameter());
        if (StrUtil.isNotBlank(token)) {
            authService.logout(token);
        }
        return success(true);
    }
}
